#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
priority_queue<ll, vector<ll>, greater<ll>> as;
int n;
int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        ll x;
        cin >> x;
        as.push(x);
    }
    ll ret = 0;
    while (as.size() > 1)
    {
        ll x = as.top();
        as.pop();
        ll y = as.top();
        as.pop();
        ret += x + y;
        as.push(x + y);
    }
    cout << ret << endl;

    return 0;
}